package com.twu.biblioteca.command;

import com.twu.biblioteca.Library.User;
import com.twu.biblioteca.service.LoginService;
import com.twu.biblioteca.view.MessageView;

import java.io.BufferedReader;

public class Login extends Command {

    private final LoginService loginService;
    private final User user;
    private final BufferedReader reader;

    public Login(LoginService loginService, User user, BufferedReader reader, MessageView messageView) {
        super(messageView);
        this.loginService = loginService;
        this.user = user;
        this.reader = reader;
    }

    @Override
    public void execute() {
        try {
            getMessageView().showMessage(MessageView.USER_ID);
            String userId = reader.readLine();
            getMessageView().showMessage(MessageView.PASSWORD);
            String password = reader.readLine();
            copy(user, loginService.login(userId, password));
        } catch (Exception e) {
            getMessageView().showMessage(MessageView.INVALID_USER);
        }
    }

    private void copy(User user, User loginUser) {
        user.setUserId(loginUser.getUserId());
        user.setUserName(loginUser.getUserName());
        user.setPassword(loginUser.getPassword());
        user.setEmail(loginUser.getEmail());
        user.setAddress(loginUser.getAddress());
        user.setPhoneNumber(loginUser.getPhoneNumber());
    }

}
